class GUIPatch

GUIPatche instances are each linked to specific Patch instances and visualize the current resource quantities as well as the number of bands present in the patch.

Constants

COLOR_AGENT_CIRCLE

Colours for agent count

COLOR_AGENT_TEXT
HEIGHT
MAX_SHADE

Colours for mini bar graph

MIN_SHADE
WIDTH

Size in pixels that the cell will request to be

Attributes

bands[R]
cereal[R]
farm[R]
prey[R]

Accessors

Public Class Methods

new(patch, shade) click to toggle source

Initialize a GUIPatch to track a certain patch. 'Shade' specifies the percent of grey to shade the widget's background.

Calls superclass method
# File lib/gui_patch.rb, line 29
def initialize(patch, shade)
  super()
  @patch = patch
  shade_range = MAX_SHADE - MIN_SHADE
  @shade = Array.new(3) { MAX_SHADE - shade * shade_range }

  # The maximum value, for vertical bar scaling
  @energy_scale = patch.resources.map{|r|r.size * r.energy_value}.max
  @size_scale = patch.resources.map{|r|r.capacity}.max

  # bar_proc derives a metric for the given resource to show on the bar graph
  # This is a default one; it will get overriden by the default radio button selection
  @bar_proc = Proc.new { |r| r.size / r.capacity }
  # This is a default scaling factor for the numbers the bar_proc spits out
  # As with the bar_proc, it will be overriden using the config_update method before any actual
  # execution
  @scale = 1

  # Initialize stores for stats
  @percentages = {}
  @bands = 0

  # Request a size and connect the draw call
  set_size_request(WIDTH, HEIGHT)
  signal_connect("expose_event") { draw }

  update
end

Public Instance Methods

config_update(conf) click to toggle source

Updates the configuration of the patch. Sets which patch value to use as the scale for the bars.

# File lib/gui_patch.rb, line 60
def config_update(conf)
  if bar_type = conf[:bar_type]
    case bar_type
    when :capacity
      @scale = 1
    when :energy
      @scale = @energy_scale
    when :size
      @scale = @size_scale
    end
  end
  if bar_proc = conf[:bar_proc]
    @bar_proc = bar_proc
  end
end
update() click to toggle source

Update the bar graph. Alternatively can set quantities individually through accessors. Prey, cereal and farm are floats between 0 and 1 (percentages)

# File lib/gui_patch.rb, line 80
def update
  redraw_necessary = false
  @patch.resources.each do |r|
    new_val = @bar_proc.call(r) / @scale
    redraw_necessary = true if !redraw_necessary && @percentages[r] != new_val
    @percentages[r] = new_val
  end
  @bands = @patch.num_bands

  queue_draw if redraw_necessary
end